window.onload = function () {
    document.getElementById("administrative").className = "add-nav-active";
    document.getElementById("document-template").className = "add_class";
    document.getElementById("organizational_management_ul").style.display = "inline-block";
    document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
    station_list();
};


// 加载流程列表
function station_list(page) {
    page = page || 1;
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/document_template.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                // window.preview_dict = JsonDate["preview_dict"];
                create_table(JsonDate["count_num"], JsonDate["data"], page);
                pagenum(JsonDate["count_num"], page);
            }
        }
    };
}


function create_table(count_num, json_data, page) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        console.log(json_data[i])
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");
        td.innerHTML = (parseInt(page)-1)*20+i+1;td.style.textAlign = "center";
        td1.innerHTML = json_data[i];td1.title=json_data[i];td1.style.textAlign = "center";
        td2.innerHTML = "<a href='#'  onclick=\"admin_add('选择模板','view-model?folder="+json_data[i]+"','800','500')\"> 查看 </a> | " +
            '<a href="#" onclick="delete_folder(\''+json_data[i]+'\')">删除</a> | ' + '<a href="#" class="file"> 上传<input type="file" name="" id="'+String((parseInt(page)-1)*20+i+1)+'" onchange="upload_file(this.id, \''+json_data[i]+'\')"></a>';td2.style.textAlign = "center";

        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);
        t_table.appendChild(tr);
        
    }
}

function upload_file(ele_id, file_path) {
    var form = new FormData();
    var files = document.getElementById(ele_id).files[0];
    form.append('files', files);
    form.append('file_path', file_path);

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;
    xhr.open('post', "/administrative/admin-add", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);

                if (JsonDate['code']===200){
                    alert("上传成功！")
                }
                else{
                    alert(JsonDate['message'])
                }

            }
        }
    }
}

function delete_folder(folder_name) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/document_template.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("folder_name="+String(folder_name)+"&page=1");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                alert("删除成功！")
                window.location.reload();
            }
        }
    };
}


// 编辑模版信息
function preview_template(ele_id) {
    var preview_dict = window.preview_dict;
    document.getElementById("model_title").innerText = "查看公文模版";
    document.getElementById("template_name").value = preview_dict[ele_id]["name"];
    document.getElementById("file-header-div").style.display = "none";
    document.getElementById("file-end-div").style.display = "none";

    var file_header = document.getElementById("file-header");
    file_header.innerText = preview_dict[ele_id]["file_header_name"];file_header.setAttribute("href", preview_dict[ele_id]["file_header_link"]);
    var file_end = document.getElementById("file-end");
    file_end.innerText = preview_dict[ele_id]["file_end_name"];file_end.setAttribute("href", preview_dict[ele_id]["file_end_link"]);
}


// 修改删除ID
function change_id(ele_id) {
    document.getElementById("countersign").setAttribute("onclick", "delete_number('"+ele_id+"')")
}

// 删除编号
function delete_number(ele_id) {
    document.getElementById("close-model2").click();
    var page = document.getElementsByClassName("active")[0].innerHTML;
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/document_template.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page)+"&delect_id="+String(ele_id));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.preview_dict = JsonDate["preview_dict"];
                create_table(JsonDate["count_num"], JsonDate["data"], page);
                pagenum(JsonDate["count_num"], page);
            }
        }
    };
}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}


// 分页
function pagenum(dataTotals, page) {
    page = page || 1;
    dataTotals = dataTotals || 0;
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            station_list(page);
        }
    });
}




function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("meeting_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("meeting_management_ul").style.display = "none";
    }
}

function click_ul3() {
    if (document.getElementById("jiantou3").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-xia";
        document.getElementById("issue_approval_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-shang";
        document.getElementById("issue_approval_ul").style.display = "none";
    }
}



// 上传附件文件头
function upload_file_header() {
    var form = new FormData();
    var files = document.getElementById("updata-file-header").files[0];
    form.append("file", files);

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/upload_file_header", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                var file_header = document.getElementById("file-header");
                file_header.innerText = JsonDate["data"][0]["accessory_name"];file_header.setAttribute("href", "/" + JsonDate["data"][0]["accessory_url"])
            }
        }
    };
}


// 上传附件文件尾
function upload_file_end() {
    var form = new FormData();
    var files = document.getElementById("updata-file-end").files[0];
    form.append("file", files);

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/upload_file_header", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                var file_end = document.getElementById("file-end");
                file_end.innerText = JsonDate["data"][0]["accessory_name"];file_end.setAttribute("href", "/" + JsonDate["data"][0]["accessory_url"])
            }
        }
    };
}


// 提交新建公文模版
function save_template(templateId) {
    templateId = templateId || "";
    var template_name = document.getElementById("template_name").value;         // 模版名称

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/add_template.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("template_name="+String(template_name));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.preview_dict = JsonDate["preview_dict"];
                if (JsonDate["code"] === 200) {
                    alert("创建成功！");
                    document.getElementById("close-model").click();
                    create_table(JsonDate["count_num"], JsonDate["data"], 1);
                    pagenum(JsonDate["count_num"], 1);

                }else {
                    alert("该文件夹已存在！")
                }
            }
        }
    };
}
